package day03;

import java.util.ArrayList;
import java.util.Random;

class GroupOwner {
    String name;
    int money;

    public GroupOwner(String name, int money) {
        this.name = name;
        this.money = money;
    }

    // 群主发红包
    public ArrayList<Integer> send(int totalMoney, int memberCount) {
        ArrayList<Integer> redPackets = new ArrayList<>();

        // 确保红包平分
        int equalShare = totalMoney / memberCount;

        // 如果总钱数无法平分，剩余的钱交给最后一个红包
        int remainingMoney = totalMoney;
        for (int i = 0; i < memberCount - 1; i++) {
            redPackets.add(equalShare);
            remainingMoney -= equalShare;
        }
        // 最后一个红包拿到剩余的钱
        redPackets.add(remainingMoney);

        // 返回红包金额列表
        return redPackets;
    }

    // 群主扣款
    public void deductMoney(int totalMoney) {
        this.money -= totalMoney;
    }

    // 输出群主的余额
    public void printBalance() {
        System.out.println(this.name + " 当前余额：" + this.money + " 元");
    }
}

class Player {
    String name;
    int money;

    // 构造方法
    public Player(String name, int money) {
        this.name = name;
        this.money = money;
    }

    // 成员抢红包
    public void receive(ArrayList<Integer> redPackets) {
        if (redPackets.isEmpty()) {
            System.out.println(this.name + " 没有红包可抢");
            return;
        }
        // 随机选择一个红包
        int randomIndex = new Random().nextInt(redPackets.size());
        int receivedMoney = redPackets.remove(randomIndex);

        // 将抢到的红包金额加入成员余额
        this.money += receivedMoney;

        // 输出抢到红包的信息
        System.out.println(this.name + " 抢到红包 " + receivedMoney + " 元，余额：" + this.money + " 元");
    }

    // 输出当前玩家的余额
    public void printBalance() {
        System.out.println(this.name + " 当前余额：" + this.money + " 元");
    }
}

public class HW_2_SendHongBao {
    public static void main(String[] args) {
        // 创建群主和三个成员
        GroupOwner groupOwner = new GroupOwner("群主", 1000);
        Player member1 = new Player("成员1", 0);
        Player member2 = new Player("成员2", 0);
        Player member3 = new Player("成员3", 0);

        // 群主发了一个300的红包，分成3份
        ArrayList<Integer> redPackets = groupOwner.send(300, 3);

        // 群主扣除发红包的金额
        groupOwner.deductMoney(300);

        // 成员抢红包
        member1.receive(redPackets);
        member2.receive(redPackets);
        member3.receive(redPackets);

        // 打印余额
        groupOwner.printBalance();
        member1.printBalance();
        member2.printBalance();
        member3.printBalance();
    }
}
